package com.homework.dianqi;


import java.util.Scanner;

/**
 * @author xiyu
 * @version 1.0
 * @date 2025/4/3 16:38
 * @description
 */
public class UserDao {

    static Scanner input = new Scanner(System.in);
    public void menu(){

        while (true){

            System.out.println("*****************电器商城***************");
            System.out.println("1.用户登录");
            System.out.println("2.用户注册");
            System.out.println("3.退出系统");
            System.out.println("*****************电器商城***************");
            System.out.print("请输入菜单：");
            int next = input.nextInt();
            switch (next){
                case 1:
                    login();
                    break;
                case 2:
                    reg();
                    break;
                default:
                    System.out.println("*****************已退出****************");
                    return;
            }
        }
    }

    //注册
    private void reg() {
        System.out.println("******开始注册******");
        String userName = "";
        boolean flag = false;
        while (true) {
            System.out.print("请输入用户名：");
            userName = input.next();
            for (User user : DataProvider.userList) {
                if (userName.equals(user.getUserName())){
                    System.out.println("用户名重复");
                }else {
                    flag = true;
                    break;
                }
            }
            if (flag){
                break;
            }
        }

        String userPassword = "";
        while (true){
            System.out.print("请输入密码：");
            userPassword = input.next();
            System.out.print("请再次输入密码：");
            String userPassword1 = input.next();
            if (userPassword.equals(userPassword1)){
                if (userPassword.length()<6){
                    System.out.println("密码长度不能小于6");

                }else {
                    break;
                }
            }else {
                System.out.println("两次输入的密码不一致");
            }
        }

        int age = 0;
        System.out.print("请输入年龄：");
        while (true){
            age = input.nextInt();
            if (age<0){
                System.out.println("您输入的年龄必须是0-100之间的数字");
                System.out.print("请重新输入年龄：");
            }else {
                break;
            }
        }

        System.out.print("请输入别名：");
        String otherName = input.next();

        DataProvider.userList.add(new User(userName,userPassword,otherName,age));
        System.out.println("******注册成功******");
    }
    //登录
    private void login() {
//        System.out.println(DataProvider.userList);
        System.out.print("请输入用户名：");
        String userName = input.next();
        System.out.print("请输入密码：");
        String userPassword = input.next();
        boolean flag = false;
        for (User user : DataProvider.userList) {
            System.out.println(user);
            if (user.getUserName().equals(userName)&&
                    user.getPassWord().equals(userPassword)){
                DataProvider.currentUser = user;//设置当前用户
                flag = true;
            }
        }
        if (flag){
            System.out.println("登陆成功  您好："+userName);
            menu1(userName);
        }else {
            System.out.println("用户名或密码不正确");
        }
    }

    //登陆成功菜单
    private void menu1(String userName) {
        while (true){

            System.out.println("*****************电器商城***************");
            System.out.println("1.商品购买");
            System.out.println("2.我的购物车");
            System.out.println("3.个人信息");
            System.out.println("4.注销");
            System.out.println("*****************电器商城***************");
            System.out.print("请输入菜单：");
            int next = input.nextInt();
            switch (next){
                case 1:
                    buy();
                    break;
                case 2:
                    shopcart();
                    break;
                case 3:
                    userinfo();
                    break;
                default:
                    //退出当前账号
                    DataProvider.currentUser = null;
                    //重置购物车
                    DataProvider.carList.clear();
                    System.out.println("*****************已退出****************");
                    return;
            }
        }
    }

    private void buy() {
        while (true){
            System.out.println("商品编号\t\t\t\t" +
                    "商品名称\t\t\t\t" +
                    "商品价格\t\t\t\t" +
                    "商品库存");
            for (Product product : DataProvider.proList) {
                System.out.println(product.getId()+"\t\t\t\t" +
                        product.getName()+"\t\t\t\t" +
                        product.getPrice()+"\t\t\t\t" +
                        product.getStock()+"\t\t\t\t");
            }
            Product p1 = new Product();

            String pid = "";
            while (true){
                System.out.print("请输入商品编号：");
                pid = input.next();
                boolean flag = false;
                for (Product product : DataProvider.proList) {
                    if (pid.equals(product.getId())){
                        //获取当前商品
                        p1 = product;
//                    System.out.println(p1);
                        flag = true;
                    }
                }
                if (flag){
                    break;
                }else {
                    System.out.println("您输入的商品编号不存在");
                }
            }

            int sl = 0;
            while (true){
                System.out.print("请输入购买数量：");
                sl = input.nextInt();
                if (sl<=0){
                    System.out.println("购买数量不能为0或负数");
                }else if (sl>p1.getStock()){
                    System.out.println("购买数量必须小于"+p1.getStock());
                }else {
                    //加入购物车
                    DataProvider.carList.add(new Product(p1.getId(),p1.getName(),p1.getPrice(),sl));

                    break;
                }
            }
            System.out.println("您还要继续购买吗？（是Y 否 任意键)");
            String next = input.next();
            if (next.equals("y")){
            }else {
                break;
            }
        }
    }



    private void shopcart() {
        System.out.println("商品编号\t\t\t\t" +
                "商品名称\t\t\t\t" +
                "商品价格\t\t\t\t" +
                "商品库存");
        double sum = 0;
        for (Product product : DataProvider.carList) {
            System.out.println(product.getId()+"\t\t\t\t" +
                    product.getName()+"\t\t\t\t" +
                    product.getPrice()+"\t\t\t\t" +
                    product.getStock()+"\t\t\t\t");
            sum = sum+(product.getPrice()*product.getStock());
        }

        System.out.println("总金额为："+sum);
        while (true){
            System.out.println("您是否付款？（是y 否 任意键）");
            String next = input.next();
            if (next.equals("y")){
                System.out.println("请输入付款金额：");
                double money = input.nextDouble();
                if (money<sum){
                    System.out.println("金额不足");
                }else {
                    System.out.println("找零："+(money-sum));
                    //扣除库存
                    for (Product carp : DataProvider.carList) {
                        for (Product p : DataProvider.proList) {
                            if (carp.getId().equals(p.getId())){
                                p.setStock(p.getStock()-carp.getStock());
                            }
                        }
                    }
                    //清空购物车
                    DataProvider.carList.clear();
                    break;
                }
            }else {
                //不付款返回上一级
                break;
            }
        }
    }

    private void userinfo() {
        User currentUser = DataProvider.currentUser;
        System.out.println("登陆名："+currentUser.getUserName());
        System.out.println("年龄："+currentUser.getAge());
        System.out.println("昵称："+currentUser.getOtherName());
    }
}
